package cn.edu.xidian.dao.Impl;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import cn.edu.xidian.dao.UserDao;
import cn.edu.xidian.domain.User;
import cn.edu.xidian.untils.SQLiteUntils;

import java.util.ArrayList;

public class UserDaoImpl implements UserDao {

    private ArrayList<User> userList = new ArrayList<User>();
    //数据库游标
    private Cursor cursor;

    /**
     * 查询用户保存的网站信息
     * @return List
     */
    @Override
    public ArrayList<User> getUserList(SQLiteDatabase db) {

        String sql = "select user_id,user_website,user_name,user_pwd from user";

        cursor = db.rawQuery(sql,null);

        cursor.moveToFirst();//游标移动到最前面
        //遍历进行保存
        while(!cursor.isAfterLast()){
            User user = new User(cursor.getString(cursor.getColumnIndex("user_id")),
                    cursor.getString(cursor.getColumnIndex("user_website")),
                    cursor.getString(cursor.getColumnIndex("user_name")),
                    cursor.getString(cursor.getColumnIndex("user_pwd")));
            userList.add(user);

            cursor.moveToNext();
        }

        return userList;
    }

    public ArrayList<User> getUserList() {
        return userList;
    }

    public void setUserList(ArrayList<User> userList) {
        this.userList = userList;
    }

    public Cursor getCursor() {
        return cursor;
    }

    public void setCursor(Cursor cursor) {
        this.cursor = cursor;
    }


    /**
     * 数据库插入操作，进行持久化
     * @param itemNums 新插入的数据条目数目
     * @param users
     * @return int 插入的数目
     */
    @Override
    public int insertToDatabase(int itemNums, ArrayList<User> users){
        if(itemNums == 0){
            //没有传入新的值
            return 0;
        }
        int insertNums = itemNums;

        StringBuilder sb = new StringBuilder("insert into user(user_id,user_website,user_name,user_pwd) values");

        for(int i=users.size()-itemNums;i<users.size();++i){
            if(itemNums > 0){
                sb.append("(\"").append(users.get(i).getUser_id()).append("\",")
                        .append("\"").append(users.get(i).getUser_website()).append("\",")
                        .append("\"").append(users.get(i).getUser_name()).append("\",")
                        .append("\"").append(users.get(i).getUser_pwd()).append("\")");
                itemNums = 0;
            }else{
                //多个数据需要在括号前面添逗号
                sb.append(",(\"").append(users.get(i).getUser_id()).append("\",")
                        .append("\"").append(users.get(i).getUser_website()).append("\",")
                        .append("\"").append(users.get(i).getUser_name()).append("\",")
                        .append("\"").append(users.get(i).getUser_pwd()).append("\")");
            }
        }
        String sql = sb.toString();
        Log.i("sql",sql);

        SQLiteUntils.getSqliteDB().execSQL(sql);

        return insertNums;
    }

    /**
     * 删除某行
     * @param id 要删除的行的id
     */
    @Override
    public void deleteFromdatabase(String id) {
        String sql;
        if("all".equals(id)){
            sql = "delete from user";
        }else{
            sql = "delete from user where user_id = \"" + id +"\"";
        }
        SQLiteUntils.getSqliteDB().execSQL(sql);
//        execute();
    }

    /**
     * 更新某行
     * @param user
     */
    @Override
    public void updateToDatabase(User user) {
        String sql = "update user set user_website = \""+user.getUser_website()
                +"\", user_name=\""+user.getUser_name()
                +"\", user_pwd=\""+user.getUser_pwd()
                +"\" where user_id = \""+user.getUser_id()+"\"";
        SQLiteUntils.getSqliteDB().execSQL(sql);
//        execute();
    }

    /**
     * 更新或者删除数据后，进行多线程操作，更新userlist和cursor
     */
    private void execute(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                userList = getUserList(SQLiteUntils.getSqliteDB());
            }
        }).start();
    }
}
